﻿#include <libcomm.h>

void __SignalHandler(int signo) 
{
	printf("SIGNAL(%d) interrupt !\n",signo);
	printf("%s",StackTracer::TraceStack().get());
	exit(0);
}


ThreadSemaphore g_sem;

static int ThreadProcTest(CommType& arg)
{
	for (int i=0;i<3;i++)
	{
		Thread::Sleep(2000);
		g_sem.SetSignal();
		printf("SetSignal: %d.\n", i);
	}
	return 0;
}

int main(int argc, char *argv[])
{
#ifndef COMM_MSVC
	struct sigaction act;
	act.sa_handler = __SignalHandler;
	MYLIB_ASSERT_ERRNO(0==sigfillset(&act.sa_mask));//forbid all signal while handler.
	act.sa_flags = 0;
	//MYLIB_ASSERT_ERRNO(0==sigaction(SIGINT,&act,NULL));
	MYLIB_ASSERT_ERRNO(0==sigaction(SIGSEGV,&act,NULL));
	MYLIB_ASSERT_ERRNO(0==sigaction(SIGILL,&act,NULL));
	MYLIB_ASSERT_ERRNO(0==sigaction(SIGFPE,&act,NULL));
	MYLIB_ASSERT_ERRNO(0==sigaction(SIGTERM,&act,NULL));
	MYLIB_ASSERT_ERRNO(0==sigaction(SIGABRT,&act,NULL));
	//signal(SIGUSR1,SIG_IGN);
#endif

	LOGDEBUG("COMLIB: %s",common_version());

	Thread* trd = Thread::Run(ThreadProcTest);
	do 
	{
		if (g_sem.WaitSignal(0))
		{
			printf("WaitSignal!\n");
			continue;
		}
		Thread::Sleep(10);
	} while (true);

	
	
	
	getchar();
	return 0;
}